package im.starchat.chat.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.rest.model.MemberInfo;
import im.starchat.chat.rest.model.UserBean;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.view.CircularImageButton;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by skyfishjy on 1/6/15.
 */
public class HostDialogFragment extends DialogFragment {
    Dialog dialog;

    private ImageView mClose;
    private CircularImageButton mHeaderCover;
    private TextView mName;
    private TextView mSex;
    private TextView mDescribe;
    private TextView mAttentionBtn;
    private TextView mCancelBtn;

    private static final String MEMBER_INFO = "member_info";
    private MemberInfo mMemberInfo;

//    public static MemberDialogFragment newInstance() {
//        MemberDialogFragment comicDetailFragment = new MemberDialogFragment();
//        return comicDetailFragment;
//    }

    public static HostDialogFragment newInstance(MemberInfo memberInfo) {
        HostDialogFragment fragment = new HostDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(MEMBER_INFO, memberInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMemberInfo = getArguments().getParcelable(MEMBER_INFO);
        if(mMemberInfo != null){
            getUserInfo(mMemberInfo.getUserPhone());
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_host, null);
        builder.setView(view);
        dialog = builder.create();
        mClose = (ImageView)view.findViewById(R.id.close);
        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mHeaderCover = (CircularImageButton)view.findViewById(R.id.avator);
        mName = (TextView)view.findViewById(R.id.name);
        mSex = (TextView)view.findViewById(R.id.sex);
        mDescribe = (TextView)view.findViewById(R.id.describe);
        mAttentionBtn = (TextView)view.findViewById(R.id.attention);
        mCancelBtn = (TextView)view.findViewById(R.id.cancel_attention);

        if(mMemberInfo != null){
            if (!TextUtils.isEmpty(mMemberInfo.getHeadImagePath())) {
                Picasso.with(getActivity()).load(mMemberInfo.getHeadImagePath())
                        .placeholder(R.drawable.home_head)
                        .into(mHeaderCover);
            }
            mName.setText(mMemberInfo.getUserName());
            mDescribe.setVisibility(View.GONE);
        }
        mAttentionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                following(mMemberInfo.getUserPhone());
            }
        });
        mCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelFollowing(mMemberInfo.getUserPhone());
            }
        });



        return dialog;
    }

    //获取明星资料
    private void getUserInfo(String userId){
        if(!TextUtils.isEmpty(userId))
            XLApplication.getInstance().getRestClient().getUserInfo(userId, new Callback<UserBean>() {
                @Override
                public void success(UserBean userBean, Response response) {
//                UserUtils.setUserInstance(userBean);
//                PreferencesStorageUtil.setUserBean(StarActivity.this, GsonUtil.toJson(userBean));
//                    mUserBean = userBean;
//                    refreshAccount(userBean);
                    if(userBean.is_following()){
                        mAttentionBtn.setVisibility(View.INVISIBLE);
                        mCancelBtn.setVisibility(View.VISIBLE);
                    }else{
                        mAttentionBtn.setVisibility(View.VISIBLE);
                        mCancelBtn.setVisibility(View.INVISIBLE);
                    }
                }

                @Override
                public void failure(RetrofitError error) {

                }
            });
    }


    private void following(String id){
        XLApplication.getInstance().getRestClient().following(id, "", new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(getActivity(), "关注成功", Toast.LENGTH_SHORT).show();
                mAttentionBtn.setVisibility(View.INVISIBLE);
                mCancelBtn.setVisibility(View.VISIBLE);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }


    private void cancelFollowing(String id){
        XLApplication.getInstance().getRestClient().delFollowing(id, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(getActivity(), "取消关注成功", Toast.LENGTH_SHORT).show();
                mAttentionBtn.setVisibility(View.VISIBLE);
                mCancelBtn.setVisibility(View.INVISIBLE);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }


}
